package com.ff.xyh.consumer.dictionary.controller;


import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.dictionary.RestrictedStatus;
import com.ff.xyh.consumer.dictionary.service.RestrictedStatusService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 限制情况表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@Api(tags = "封号，限制状态")
@RestController
@RequestMapping("/dictionary")
public class RestrictedStatusController {
    @Autowired
    private RestrictedStatusService restrictedStatusService;

    @GetMapping("get-restricted-status-all")
    public ResultObject getAllRestrictedStatus() {
        List<RestrictedStatus> list = restrictedStatusService.list(null);
        if (list == null || list.isEmpty()){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("items", list);
            return ResultObject.ok().data(map);
        }
    }

    @GetMapping("get-restricted-status-id")
    public ResultObject getRestrictedStatusById(
            @RequestParam(name = "id") Integer id) {
        RestrictedStatus restrictedStatus = restrictedStatusService.getById(id);
        if (restrictedStatus == null){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("data", restrictedStatus);
            return ResultObject.ok().data(map);
        }
    }

    @GetMapping("delete-restricted-status-id")
    public ResultObject deleteRestrictedStatusById(
            @RequestParam(name = "id") Integer id) {
        RestrictedStatus restrictedStatus = restrictedStatusService.getById(id);
        if (restrictedStatus == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (restrictedStatusService.removeById(id)) {
                Map map = new HashMap();
                map.put("data", restrictedStatus);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        }
    }

    @PostMapping("insert-restricted-status")
    public ResultObject insertRestrictedStatus (
            @RequestBody RestrictedStatus restrictedStatus) {
        if (restrictedStatusService.getById(restrictedStatus.getId()) != null){
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_EXISTS);
        } else {
            if (restrictedStatusService.save(restrictedStatus)) {
                Map map = new HashMap();
                map.put("data", restrictedStatus);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT_BUT_FAILED_TO_INSERT);
            }
        }
    }

    @PostMapping("update-restricted-status")
    public ResultObject updateRestrictedStatus(
            @RequestBody RestrictedStatus restrictedStatus) {
        if (restrictedStatusService.getById(restrictedStatus.getId()) == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (restrictedStatusService.updateById(restrictedStatus)) {
                Map map = new HashMap();
                map.put("data", restrictedStatus);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_UPDATE);
            }
        }
    }
}

